package com.coder520.sync_learning.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class FairAndUnfairTest {
	private static Lock fairLock=new ReentrantLock2(true);
	
	private static Lock unFairLock=new ReentrantLock2(false);

	private static class Job extends Thread{
		private Lock lock;
		public Job(Lock lock) {
			// TODO Auto-generated constructor stub
			this.lock=lock;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			System.out.println(Thread.currentThread().getName());
		}
	}
	
	private static class ReentrantLock2 extends ReentrantLock{
		public ReentrantLock2(boolean fair) {
			// TODO Auto-generated constructor stub
			super(fair);
		}
		
		@Override
		protected Collection<Thread> getQueuedThreads() {
			// TODO Auto-generated method stub
			List<Thread> arrayList=new ArrayList<Thread>(super.getQueuedThreads());
			
			Collections.reverse(arrayList);
			
			return arrayList;
		}
	}
}
